/*
 * @lc app=leetcode.cn id=129 lang=javascript
 *
 * [129] 求根到叶子节点数字之和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumNumbers = function(root) {
    const dfs = function(sum, node) {
        if (!node) return 0
        sum = sum * 10 + node.val
        if (!node.left && !node.right) return sum
        return dfs(sum, node.left) + dfs(sum, node.right)
    }
    return dfs(0, root)
};
// @lc code=end

